package com.tjh.gulimall.order.web;

import com.alipay.api.AlipayApiException;
import com.tjh.gulimall.order.config.AlipayTemplate;
import com.tjh.gulimall.order.service.OrderService;
import com.tjh.gulimall.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Create by koala on 2021-05-29
 */
@Controller
public class PayWebController {

    @Autowired
    AlipayTemplate alipayTemplate;

    @Autowired
    OrderService orderService;

    /**
     * day132：支付宝支付
     *      1、将支付页让浏览器展示
     *      2、支付成功以后，我们要跳到用户的订单列表页
     * 测试：
     *      1、浏览器访问购物车页面（登录状态）：http://cart.gulimall.com/cart.html
     *      2、点击结算按钮去结算
     *      3、进入结算页
     *      4、点击提交订单按钮，进入支付页
     *      6、点击支付宝进行支付
     *      7、调起支付宝支付页面说明该方法是成功的
     */
    @ResponseBody
    @GetMapping(value = "/payOrder",produces = "text/html")
    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
        PayVo payVo = orderService.getOrderPay(orderSn);
        //返回的是一个页面。将此页面直接交给浏览器就行
        String pay = alipayTemplate.pay(payVo);
        System.out.println(pay);

        return pay;
    }

}